package ImageUtil;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;

public class ImageReplaceUtils {


    /**
     * 根据id删除一个ExamGuide
     * @param backgroundPath 背景图绝对路径
     * @param qrCodePath      内容图绝对路径
     * @param underPath       白板绝对路径
     * @param medalPathArrray  勋章绝对路径数组
     * @param ossMap           oss相关配置集合Map<String,String> websiteMap = websiteProfileService.getWebsiteProfileByType(WebSiteProfileType.oss.toString());
     */
    public static String overlapImage(String backgroundPath,String qrCodePath,String underPath,String message01,String[] medalPathArrray,Map<String,String> ossMap){
        try {
            String rootPath = System.getProperty("rootPath");//根目录
            /**
             * 调整背景图，内容图，白板图大小，获取对象
             * */
            Image back = ImageIO.read(new File(backgroundPath));
            Image picture = ImageIO.read(new File(qrCodePath));
            Image xia = ImageIO.read(new File(underPath));
            BufferedImage background = resizeImage(xia.getWidth(null),back.getHeight(null)-320, ImageIO.read(new File(backgroundPath)));
            BufferedImage qrCode = resizeImage(picture.getWidth(null),picture.getHeight(null), ImageIO.read(new File(qrCodePath)));
            BufferedImage under = resizeImage(xia.getWidth(null),xia.getHeight(null),ImageIO.read(new File(underPath)));
            /**
             * 向内容图上写名字，将勋章图和内容图结合
             * */
            Graphics2D q = qrCode.createGraphics();
            q.setColor(Color.BLACK);
            q.setFont(new Font("宋体",Font.BOLD,50));
            q.drawString(message01, 150,qrCode.getHeight()/2 + 130);
//            if(ObjectUtils.isNotNull(medalPathArrray)){
                int medalSpace = 60;
                for(int i = 0 ; i < medalPathArrray.length && i<7; i++){
                    BufferedImage medalImage = resizeImage(45,45, ImageIO.read(new File(medalPathArrray[i])));
                    if(i==0){
                        q.drawImage(medalImage, qrCode.getWidth()/2-40, qrCode.getHeight()/5*4 + 50, medalImage.getWidth(), medalImage.getHeight(), null);
                    }else if(i < 3){
                        q.drawImage(medalImage, (qrCode.getWidth()/2)-(40 + i*medalSpace), qrCode.getHeight()/5*4 + 50, medalImage.getWidth(), medalImage.getHeight(), null);
                    }else if(i==3){
                        q.drawImage(medalImage, qrCode.getWidth()/2-8, qrCode.getHeight()/5*4 + 100, medalImage.getWidth(), medalImage.getHeight(), null);
                    }else if(i>3){
                        q.drawImage(medalImage, (qrCode.getWidth()/2)-(8 + (i-3)*medalSpace), qrCode.getHeight()/5*4 + 100, medalImage.getWidth(), medalImage.getHeight(), null);
                    }
                }
//            }
            q.dispose();
            //内容图和背景图结合
            Graphics2D b = background.createGraphics();
            b.drawImage(qrCode, (background.getWidth()-qrCode.getWidth()) / 2, (background.getHeight()-qrCode.getHeight())/2, qrCode.getWidth(), qrCode.getHeight(), null);
            b.dispose();
            //背景图和白板图结合
            Graphics2D u = under.createGraphics();
            u.setColor(Color.WHITE);
            u.drawImage(background, (under.getWidth()-background.getWidth())/2, 0, background.getWidth(), background.getHeight(), null);
            u.dispose();
            //随机生成名字，上传oss
//            String url = rootPath + AutoIdGenerator.INSTANCE.nextId() + ".png";
//            ImageIO.write(under, "png", new File(url));
//            String urls = GetStarted.ossAliYunUploadSSS(ossMap.get("oss_endPoint"),ossMap.get("oss_id"),ossMap.get("oss_secret"),ossMap.get("img_bucket"),url);
//            //删除本地文件
//            FileUtils.deleteFile(url);
            //返回路径
//            return urls;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static BufferedImage resizeImage(int x,int y,BufferedImage bfi){
        BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_ARGB);
        bufferedImage.getGraphics().drawImage(
                bfi.getScaledInstance(x, y, Image.SCALE_SMOOTH), 0, 0, null);
        return bufferedImage;
    }





}
